package com.weaver.seconddev.custom.hook;


import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity;
import com.weaver.ebuilder.datasource.api.enums.SourceType;
import com.weaver.ebuilder.datasource.api.service.DataSetService;
import com.weaver.loom.api.annotation.RegHook;
import com.weaver.loom.context.register.AbstractRegHookEvent;
import com.weaver.mc.api.entity.SendMessageEntity;
import com.weaver.verupgrade.general.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 消息处理钩子
 * @author: chenwh
 * @created: 2024/01/25 15:27
 */
@Slf4j
@RegHook(module = "workflow", event = "requestSendMessageEvent", order = 0)
public class SendMessageEntityHandleHook extends AbstractRegHookEvent {
    public SendMessageEntityHandleHook () {
        log.error("=====SendMessageEntityHandleHook===init===");
    }
    
    
    @Autowired
    private DataSetService dataSetService;
    
    
    
    public List<LinkedHashMap<String, Object>> getRelationConfigList(String sql ,String service){
        ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
        executeSqlEntity.setSql(Base64.encode(sql));
        //groupid,可以访问  E10地址/api/datasource/ds/group?sourceType=LOGIC 获取
        executeSqlEntity.setGroupId(service);
        executeSqlEntity.setSourceType(SourceType.LOGIC);
        Map<String, Object> datas = dataSetService.executeSql(executeSqlEntity);
        log.error("=====SendMessageEntityHandleHook消息处理钩子===datas{}",datas);
        //返回值的records是一个map集合
        List<LinkedHashMap<String, Object>> relationConfigList = (List<LinkedHashMap<String, Object>>)datas.get("records");
        log.error("=====SendMessageEntityHandleHook消息处理钩子===relationConfigList{}",relationConfigList);
        return relationConfigList ;
    }
    
    
    @Override
    public Object handle(Object[] args) throws Exception {
        Map<String,Object> result =new HashMap();
        
        log.error("=====SendMessageEntityHandleHook消息处理钩子=New==args{}===",args);
        if(args.length >1){
            List delReceivedIds = new ArrayList();
            // 参数1：SendMessageEntity实体JSON串
            String entityStr = (String) args[0];
            log.error("=====SendMessageEntityHandleHook消息处理钩子=new==entityStr===",entityStr);
            SendMessageEntity sendMessageEntity= JSONObject.parseObject(entityStr, SendMessageEntity.class);
            // 参数2：流程ID
            long requestId = (Long) args[1];
            log.error("=====SendMessageEntityHandleHook消息处理钩子===requestId==="+requestId);
            String currentnodeid = "";
            String operate_log_id = "";
            String sql2 = "select nodeid,nodetype,operate_log_id from wfc_currentnode where requestid='"+requestId+"' and delete_type=0";
            List<LinkedHashMap<String, Object>> relationConfigList2 = getRelationConfigList( sql2 , "weaver-workflow-list-service");
            if(null!=relationConfigList2) {
                for (int i = 0; i < relationConfigList2.size(); i++) {
                    LinkedHashMap<String, Object> records = relationConfigList2.get(i);
                    currentnodeid = Util.null2String(records.get("nodeid"));
                    operate_log_id = Util.null2String(records.get("operate_log_id"));
                }
            }
            log.error("=====SendMessageEntityHandleHook消息处理钩子===currentnodeid:{}",currentnodeid);
            
            String groupdetailid = "";
            String findFilterSql = "select nodegroupdetail_id from uf_notificationblock where block_mail = '0'";
            List<LinkedHashMap<String, Object>> findFilterList = getRelationConfigList( findFilterSql , "weaver-ebuilder-form-service");
            if(null!=findFilterList) {
                for (int i = 0; i < findFilterList.size(); i++) {
                    LinkedHashMap<String, Object> records = findFilterList.get(i);
                    String nodegroupdetail_id = Util.null2String(records.get("nodegroupdetail_id"));
                    if(!nodegroupdetail_id.equals("")) {
                        if (groupdetailid.equals("")) {
                            groupdetailid = nodegroupdetail_id;
                        } else {
                            groupdetailid += "," + nodegroupdetail_id;
                        }
                    }
                }
            }
   
            log.error("=====SendMessageEntityHandleHook消息处理钩子===groupdetailid:{}",groupdetailid);
            
            String filterUserSql = " select   user_master_id from wfc_operate where requestid='"+requestId+"'  and delete_type=0  and optgroupdetailid in("+groupdetailid+") ";
            log.error("=====SendMessageEntityHandleHook消息处理钩子===filterUserSql:{}",filterUserSql);
            List<LinkedHashMap<String, Object>> filterUserList = getRelationConfigList( filterUserSql , "weaver-workflow-list-service");
            if(null!=filterUserList) {
                for (int i = 0; i < filterUserList.size(); i++) {
                    LinkedHashMap<String, Object> records = filterUserList.get(i);
                    String user_master_id = Util.null2String(records.get("user_master_id"));
                    if(!delReceivedIds.contains(user_master_id)) {
                        delReceivedIds.add(Util.null2String(records.get("user_master_id")));
                    }
                }
            }
            log.error("=====SendMessageEntityHandleHook消息处理钩子===delReceivedIds:{}",delReceivedIds);
            if(delReceivedIds.size()>0){
                //过滤提醒人
                result.put("delReceivedIds",delReceivedIds);
            }
        }
        // 返回结果,如果要阻断消息发送；则在result.put("status",false);
        log.error("=====SendMessageEntityHandleHook消息处理钩子===result:{}"+result);
        return result;
    }
}